package bg.btanev.eightyfour.gwtfacade.commandhandler;

import java.io.Serializable;

import bg.btanev.eightyfour.shared.command.CommandAction;
import bg.btanev.eightyfour.shared.command.CommandInput;
import bg.btanev.eightyfour.shared.command.CommandResult;

public abstract class GenericCommandHandler<I extends CommandInput, R extends Serializable, CR extends CommandResult<I, R>, CA extends CommandAction<I, CR>>
		implements CommandHandler<I, R, CR, CA> {

	@Override
	public final CR execute(CA commandAction) {
		I attachedInput = attach(commandAction.getInput());
		R result = doExecuteInternal(attachedInput);
		R detachedResult = detach(result);

		CR commandResult = packCommandResult(commandAction.getInput(), detachedResult);

		return commandResult;
	}

	protected abstract CR packCommandResult(I input, R detachedResult);

	protected abstract R doExecuteInternal(I attachedInput);
}